package com.yiren.dbaa.module.practice_set.carmodel.model;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.currency.bean.LinePointBean;
import com.yiren.backstage.currency.entity.CarModelEntity;
import com.yiren.backstage.currency.utils.DateUtil;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.carmodel.contract.CarModelReleaseContract;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.bean.TestBaseBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lwb
 * @date : 2022/12/6 15:48
 * @description: 车模发布
 */
public class CarModelReleaseModel implements CarModelReleaseContract.Model {

    private Activity activity;
    private Gson gson;

    public CarModelReleaseModel(Activity activity) {
        this.activity = activity;
        gson = new Gson();
    }

    @Override
    public void publishCarModel(CarModelEntity carModel, String userId, String updateDetails, int updateMode, CommonCallBack callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", carModel.getId());
        map.put("name", carModel.getName());
        map.put("antennaType", carModel.getAntennaType());
        map.put("positioned", carModel.getPositioned());
        map.put("userId", userId);
        map.put("carModelVer", carModel.getCarModelVerLast() + 1);
        map.put("useRange", carModel.getUseRange());
        map.put("updateDetails", updateDetails);
        map.put("updateMode", updateMode);
        map.put("publishTime", DateUtil.getCurrentDate());
        map.put("state", 1);
        map.put("antennaHeight", carModel.getAntennaHeight());
        map.put("antennaAnglepitch", carModel.getAntennaAnglepitch());
        map.put("antennaDirection", carModel.getAntennaDirection());
        map.put("antennaDirectionValue", carModel.getAntennaDirectionValue());
        map.put("antennaPosition", carModel.getAntennaPosition());
        map.put("antennaPositionValue", carModel.getAntennaPositionValue());
        if (carModel.getPoints() == null) {
            map.put("points", "");
        } else {
            map.put("points", carModel.getPoints());
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                String line = OkHttpUtils.getInstance().z(AddressConstant.publishCarModel().getUrl(), new Gson().toJson(map));
                if (!StringUtil.isEmpty(line)) {
                    TestBaseBean<String> testBaseBean = gson.fromJson(line, new TypeToken<TestBaseBean<String>>() {
                    }.getType());
                    if (testBaseBean.isSuccess()) {
                        callBack.success(testBaseBean.getData());
                    } else {
                        callBack.fail("发布失败!");
                    }
                } else {
                    callBack.fail("发布失败!");
                }
            }
        }).start();
    }

}